﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class ReportHistory: IReportHistory
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public ReportHistory(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.ReportHistory GetReportHistory(System.Int32 reportID)
		{
			BEGDC.PreContract.DAL.ReportHistory reportHistory = BEGDC.PreContract.DAL.ReportHistory.TryFind(reportID);

            return reportHistory;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetReportHistory_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM ReportHistory";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteReportHistory(System.Int32 reportID)
		{
			try
            {
				BEGDC.PreContract.DAL.ReportHistory reportHistory = BEGDC.PreContract.DAL.ReportHistory.TryFind(reportID);
                reportHistory.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ReportHistory", string.Format("Error occured when deleting the ReportHistory, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddReportHistory(Nullables.NullableDateTime reportDate, System.String reportType, System.String reportFileName, System.String reportContent)	
		{
			try
            {
				BEGDC.PreContract.DAL.ReportHistory reportHistory = new BEGDC.PreContract.DAL.ReportHistory();
				
				reportHistory.ReportDate = reportDate;
				reportHistory.ReportType = reportType;
				reportHistory.ReportFileName = reportFileName;
				reportHistory.ReportContent = reportContent;
				reportHistory.CreateAndFlush();
				
				return reportHistory.ReportID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ReportHistory", string.Format("Error occured when adding a new ReportHistory, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateReportHistory(System.Int32 reportID, Nullables.NullableDateTime reportDate, System.String reportType, System.String reportFileName, System.String reportContent)
		{
			try
            {
                BEGDC.PreContract.DAL.ReportHistory reportHistory = GetReportHistory(reportID);

				reportHistory.ReportDate = reportDate;
				reportHistory.ReportType = reportType;
				reportHistory.ReportFileName = reportFileName;
				reportHistory.ReportContent = reportContent;
				reportHistory.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("ReportHistory", string.Format("Error occured when updating the ReportHistory, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



